<?php

namespace App\Services;

use Lcobucci\JWT\Builder;

class TokenService
{
    /**
     * 生成签名
     */
    public static function getSign(string $appid, string $secret, int $timestamp): string
    {
        return sha1($secret . md5($appid) . $timestamp);
    }

    /**
     * 获取token
     *
     * @param string $permitted 允许地址
     * @param string $appid     appid
     * @param string $secret    secret
     *
     * @throws
     *
     * @return string
     */
    public static function getToken(string $permitted, string $appid, string $secret): string
    {
        $time = time();

        return (new Builder())->issuedBy(env('APP_URL'))
            ->permittedFor($permitted)
            ->identifiedBy($appid, true)
            ->issuedAt($time)->canOnlyBeUsedAfter($time + 60)
            ->expiresAt($time + 7200)
            ->withClaim('secret', $secret)->getToken();
    }
}
